<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.

include( "../sites/${_GET['site']}/config/config.php" );

function admin_login(){

    if (!isset($_SERVER['PHP_AUTH_USER'])) {
        header('WWW-Authenticate: Basic realm="Please login with your username and password"');
        header('HTTP/1.0 401 Unauthorized');
        die('Please provide username and password!');
    } else {
        global $sql_hostname, $sql_database, $admin, $sql_username, $sql_password;
        try {
            $db = new PDO( "mysql:host=$sql_hostname;dbname=$sql_database", $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] );
            $sql_username = $_SERVER['PHP_AUTH_USER'];
            $sql_password = $_SERVER['PHP_AUTH_PW'];
        }
        catch(PDOException $e) {
            header('WWW-Authenticate: Basic realm="Wrong username/password\nPlease login with your username and password"');
            header('HTTP/1.0 401 Unauthorized');
            die('XPlease provide username and password!');
        }
    }
}

include( "../includes/utils.php" );

include( "../includes/sql.php" );


admin_login();

$db = new SQL($sql_username, $sql_password, $sql_hostname, $sql_database);

function install_config(){
    // create config table
    global $db;

    $db->exec( "CREATE TABLE IF NOT EXISTS config (name text, value text)" );

    // add default settings
    $db->exec( "INSERT INTO config (name, value) VALUES (\"site_template\", \"default\") ");
    $db->exec( "INSERT INTO config (name, value) VALUES (\"site_title\", \"My Homepage\") ");
    $db->exec( "INSERT INTO config (name, value) VALUES (\"site_default\", \"0\") ");
    $db->exec( "INSERT INTO config (name, value) VALUES (\"site_image\", \"\") ");
    $db->exec( "INSERT INTO config (name, value) VALUES (\"site_logo\", \"\") ");
    $db->exec( "INSERT INTO config (name, value) VALUES (\"site_mail\", \"\") ");
    $db->exec( "INSERT INTO config (name, value) VALUES (\"site_gtrans\", \"0\") ");
}

function install_menu(){
    global $db;
    $db->exec( "CREATE TABLE IF NOT EXISTS menu (position int, title text, id int, lang text, hidden int)" );
    $db->exec( "INSERT INTO menu (position, title, id, lang) VALUES (0, \"default\", 0, \"en\", 0) ");
    $db->exec( "ALTER TABLE menu ADD parent INT NOT NULL DEFAULT '-1' ");
}

function install_page(){
    global $db;
    $db->exec( "CREATE TABLE IF NOT EXISTS pages (id int, title text, content text, lang text)" );
    $db->exec( "INSERT INTO pages (id, title, content, lang) VALUES (0, \"default\", \"Welcome!\", \"en\") ");
}

install_config();
echo "config installed";
install_menu();
echo "menu installed";
install_page();
echo "page installed";
?>
